package com.vecspace.data.numerical;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DecimalFormat;

import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

@JsonSerialize(using = HNumericalJsonSerializer.class)  
@JsonDeserialize(using = HNumberJsonDeserializer.class) 
public class HNumber extends HNumerical{
	private static final long serialVersionUID = -1455765139905057507L;
		
	public HNumber() {
		super();
	}
		
	public HNumber(BigDecimal value) {
		this.setValue(value);
	}
	
	public String getText() {
		if(getValue() == null)return "";
		EnumNumericalSeparatorDigit separatorDigit = this.getSeparatorDigit();
		DecimalFormat df = null;
		if(EnumNumericalSeparatorDigit.QianWeiSplit == separatorDigit) {
//			df = new DecimalFormat(",###,##0.###");//保留3位小数
			df = new DecimalFormat(",###,##0.##");//保留2位小数
		}else if(EnumNumericalSeparatorDigit.WanWeiSplit == separatorDigit) {
//			df = new DecimalFormat(",####,###0.###");//保留3位小数
			df = new DecimalFormat(",####,###0.##");//保留2位小数
		}else {
//			df = new DecimalFormat("#0.###");//保留4位小数
			df = new DecimalFormat("#0.##");//保留2位小数
		}
		
		df.setRoundingMode(RoundingMode.HALF_UP);
		String text = df.format(getValue());
		return text;
	}

	@Override
	public void setText(String text) {
		BigDecimal num = this.parseDouble(text);
		this.setValue(num);
	}
	
	@Override
	public Class<HNumber> returnedClass() {
		return HNumber.class;
	}

	@Override
	public Object nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner)
			throws HibernateException, SQLException {
		BigDecimal value = rs.getBigDecimal(names[0]);
		HNumber obj = new HNumber(value);
		return obj;
	}

	@Override
	public Object deepCopy(Object value) throws HibernateException {
		if(value == null)return null;
		HNumber obj1 = (HNumber)value;
		HNumber obj2 = new HNumber(obj1.getValue());
		obj2.setSeparatorDigit(obj1.getSeparatorDigit());
		return obj2;
	}
	
	/**两个数相加，null相当于0
	 * 
	 * @param num1
	 * @param num2
	 * @return 
	 * @author: wanghua
	 */
	public static HNumber add(HNumber num1, HNumber num2) {
		BigDecimal decimal = HNumerical.add((HNumerical)num1, (HNumerical)num2);
		return new HNumber(decimal);
	}
	
	/**两个数相减，null相当于0
	 * 
	 * @param num1
	 * @param num2 
	 * @author: wanghua
	 */
	public static HNumber subtract(HNumber num1, HNumber num2) {
		BigDecimal decimal = HNumerical.subtract((HNumerical)num1, (HNumerical)num2);
		return new HNumber(decimal);
	}
	
	/** 判断num1是否大于0，num1为null返回false
	 * 
	 * @param num1
	 * @return 
	 * @author: wanghua
	 */
	public static boolean greaterThanZero(HNumber num1) {
		if(num1 == null || num1.getValue() == null)return false;
		return num1.getValue().doubleValue() > 0;
	}
	
	/** 判断num1是否大于等于0，num1为null返回false
	 * 
	 * @param num1
	 * @return 
	 * @author: wanghua
	 */
	public static boolean greaterOrEqualZero(HNumber num1) {
		if(num1 == null || num1.getValue() == null)return false;
		return num1.getValue().doubleValue() > -0.000000001;
	}
	
	public static HNumber ZERO = new HNumber(BigDecimal.ZERO);
}
